﻿using B.S.DataBase.MyProject.Domain;
using B.S.DataBase.MyProject.ErrorCode;
using B.S.DataBase.MyProject.Infrastructure.Interface;
using B.S.DataBase.MyRead.API.Appicant.Command.RoleCommand;
using MediatR;

namespace B.S.DataBase.MyRead.API.Appicant.CommandHandler.RoleCommandHandler
{
    public class RoleFanTianCommandHandler : IRequestHandler<RoleFanTianCommand, APIResult<Role>>
    {
        private readonly IBaseRepository<Role> roleRepos;

        public RoleFanTianCommandHandler(IBaseRepository<Role> roleRepos)
        {
            this.roleRepos = roleRepos;
        }

        public Task<APIResult<Role>> Handle(RoleFanTianCommand request, CancellationToken cancellationToken)
        {
            APIResult<Role> result = new APIResult<Role>();
            var obj = roleRepos.GetAll().Result.FirstOrDefault(x => x.RoleId == request.RoleId);
            if (obj == null)
            {
                result.Code = APIEnum.查询失败;
                result.Msg = APIEnum.查询失败.ToString();
            }
            else
            {
                result.Code = APIEnum.查询成功;
                result.Msg = APIEnum.查询成功.ToString();
                result.data = obj;
            }
            return Task.FromResult(result);
        }
    }
}
